<template>
	<uni-loading v-if="loading"></uni-loading>
	<view class="body" v-else>
		<!--  顶部  -->
		<top-tab v-if="stockName" readonly :options="stockList" :stockNameProp="stockName" :locationName="locationName"
			@stockChange="stockChange" @handleBarnChoose="handleBarnChoose"></top-tab>
		<!--  列表  -->
		<template v-if="purchaseList && purchaseList.length > 0">
			<template v-for="item in purchaseList">
				<view class="panel" @click="openDetail(item)">
					<view class="panel_title">
						<view class="panel_title_left">{{ item.materialName }}</view>
					</view>
					<view class="panel_content">
						<view class="panel_content">
							<view class="panel_content_row">
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">编码：</view>
									<view class="panel_content_row_item_value">{{ item.materialNo }}</view>
								</view>
							</view>
						</view>
						<view class="panel_content_row">
							<view class="panel_content_row_item">
								<view class="panel_content_row_item_title">批次：</view>
								<view class="panel_content_row_item_value">{{ item.batchNo }}</view>
							</view>
							<view class="panel_content_row_item">
								<view class="panel_content_row_item_title">规格：</view>
								<view class="panel_content_row_item_value">{{ item.materialSpec }}</view>
							</view>
						</view>
					</view>
					<view class="panel_dashboard">
						<view class="panel_dashboard_item">
							<view class="panel_dashboard_item_number success">{{ item.nowBoxCount }}</view>
							<view class="panel_dashboard_item_value">箱数</view>
						</view>
						<view class="panel_dashboard_item">
							<view class="panel_dashboard_item_number error">{{ item.nowCount }}{{ item.unit }}
							</view>
							<view class="panel_dashboard_item_value">总重</view>
						</view>
					</view>
				</view>
			</template>
		</template>
		<uni-empty-info v-else label="暂无物料信息"></uni-empty-info>

		<!--  底部按钮  -->
		<view class="footerPanel">
			<view class="footerPanel_body">
				<button @click="handleGotoTarget" class="footerBtn save"><i class="iconfont icon-duihao btn_icon"></i>完成
				</button>
				<button @click="handleScanCode" class="footerBtn scan" type="primary"><i
						class="iconfont icon-saomiao btn_icon"></i>扫码
				</button>
			</view>
		</view>

		<!-- 弹出层 -->
		<MaterialPopup ref="scaningPopup" title="物料入库" type="transferEdit" @success="initData"></MaterialPopup>
	</view>
</template>
<script>
	import topTab from "@/components/top-tab/top-tab"
	import uniLoading from "../../vBase/components/uni-loading/uni-loading"
	import uniEmptyInfo from "../../vBase/components/uni-empty-info/uni-empty-info"
	import MaterialPopup from "@/components/popup/materialPopup"

	export default {
		components: {
			topTab,
			uniLoading,
			uniEmptyInfo,
			MaterialPopup,
		},
		data() {
			return {
				loading: false,
				purchaseList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#FF4E74'
					}
				}],
				scaningShow: false,
				scanNo: null,
				isFouce: false,
				stockList: [],
				stockNo: '',
				stockName: '',
				locationNo: '',
				locationName: '',
				isSource: true,
				billNo: '',
			}
		},
		async onShow() {
			await this.getStockList();
			this.initData()
		},
		onLoad(e) {
			this.billNo = e?.billNo ?? ""
		},
		onUnload() {
			uni.$off('deleteItem');
		},
		mounted() {
			let self = this
			uni.$on('deleteItem', ({
				material,
				success
			}) => {
				uni.showModal({
					title: '提示',
					content: '确认删除吗？',
					success(res) {
						if (res.confirm) {
							self.$api.deleteAllocationMaterialBox({
								id: material.id
							}, {
								loading: true
							}).then((res) => {
								if (!res.res) return;
								self.initData();
								success && success();
							});
						}
					}
				})
			})
		},
		methods: {
			/** 查询仓库*/
			async getStockList() {
				this.loading = true;
				try {
					let {
						res,
						stockList = []
					} = await this.$api.getCustomerStock();
					if (!res) return;
					this.stockList = stockList;
				} finally {
					this.loading = false;
				}
			},
			/** 初始化数据 */
			initData() {
				this.pageConfig = {
					pageIndex: 0,
					pageSize: 20,
					bottomEnd: false,
					bottomLoading: true
				}
				this.getDataList()
			},

			/**
			 * 详情列表
			 */
			async openDetail(material) {
				let {
					res,
					data = []
				} = await this.$api.getAllocationTaskDetailBox({
					id: material.id,
				}, {
					loading: true
				});
				let dataList = data.map(item => {
					return {
						id: item.id,
						materialName: material.materialName,
						materialNo: material.materialNo,
						stockNo: material.stockNo,
						batchNo: material.batchNo,
						boxNo: item.boxNo,
						count: item.count,
						unit: item.unit,
					}
				})
				this.openPopNVue('入库明细', dataList)
			},

			/** 获取数据 */
			async getDataList() {
				let {
					res,
					data = [],
					toLocationNo,
					toStockNo
				} = await this.$api.getAllocationTaskDetail({
					billNo: this.billNo
				});
				if (!res) return;
				this.purchaseList = data;
				this.loading = false;
				this.pageConfig.bottomEnd = true;
				this.pageConfig.bottomLoading = false;
				this.stockNo = toStockNo;
				let current = this.stockList.find((option) => {
					return option.stockNo.toString() === toStockNo.toString()
				})
				if (current) this.stockName = current.stockName
				this.locationNo = toLocationNo;
				this.locationName = toLocationNo;
				uni.stopPullDownRefresh();
			},

			/**
			 * 选择
			 */
			handleBarnChoose() {
				this.$refs.barnPopup.open(this.stockNo);
			},

			/** 完成 */
			async handleGotoTarget() {
				let res = await this.$api.auditUserAllocation({
					billNo: this.billNo
				})
				if (!res.res) return
				uni.showToast({
					title: "调拨完成",
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			},

			/** 扫码 */
			async handleScanCode() {
				this.$refs.scaningPopup.open(this.stockNo, this.locationNo)
			},

			/**
			 * 删除物料
			 * @param {Object} e
			 */
			handleDelete(material) {
				const self = this
				uni.showModal({
					title: '提示',
					content: '是否删除物料？',
					success(res) {
						if (res.confirm) {
							self.$api.deleteAllocationMaterial({
								id: material.id
							}, {
								loading: true
							}).then(res => {
								if (res.res) {
									uni.showToast({
										title: '物料删除成功',
										icon: 'none'
									})
									uni.removeStorageSync("transfer")									
									self.initData();
								}
							})
						}
					}
				})
			},

			/**
			 * 下拉刷新数据
			 */
			onPullDownRefresh() {
				this.initData()
			},

			/** 上滑加载 */
			onReachBottom() {
				if (!this.pageConfig.bottomEnd) {
					this.pageConfig.pageIndex++
					this.pageConfig.bottomLoading = true
					this.getDataList();
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	@import "../../static/css/common";
</style>
